package com.wedo.mall.widget;


import android.annotation.SuppressLint;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by e on 2017/7/6.
 * 图片自适应解决方案
 * http://blog.csdn.net/smileiam/article/details/72123227
 */
public class EWebView {

    private WebView wv_web;
    private LinearLayout ll_web;

    public EWebView(LinearLayout ll_web) {
        this.ll_web = ll_web;
        init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init(){
        wv_web = new WebView(ll_web.getContext().getApplicationContext());
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
        ll_web.addView(wv_web, params);

        WebSettings settings = wv_web.getSettings();
        settings.setLoadsImagesAutomatically(true);
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);
        settings.setSupportZoom(true);
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setPluginState(WebSettings.PluginState.ON);
        //settings.setUseWideViewPort(true);

        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        wv_web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });

        wv_web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }


    public void url(String url) {
        wv_web.loadUrl(url);
    }

    public void html(String html){
        wv_web.loadData(html, "text/html", "UTF-8");
    }

    public void data(String data){
        //String html = auto(data);
        wv_web.loadDataWithBaseURL(null,data, "text/html", "UTF-8",null);
    }

    public void onDestroy() {
        if (wv_web != null) {

            ViewParent parent = wv_web.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(wv_web);
            }

            wv_web.stopLoading();
            wv_web.getSettings().setJavaScriptEnabled(false);
            wv_web.clearHistory();
            wv_web.clearView();
            wv_web.removeAllViews();
            try {
                wv_web.destroy();
                wv_web = null;
            } catch (Exception e) {
                return;
            }

        }
    }

    /**使用Jsoup解决网页图片自适应*/
    public static String auto(String html){
        try {
            Document doc= Jsoup.parse(html);
            Elements elements=doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width","100%").attr("height","auto");
            }
            return doc.toString();
        } catch (Exception e) {
            return html;
        }
    }
}

